package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcChannel;
import com.csun.cmny.provider.model.domain.OmcFollowUp;
import com.csun.cmny.provider.model.dto.channel.ChannelQueryDto;
import com.csun.cmny.provider.model.dto.channel.channelSaveDto;
import com.csun.cmny.provider.model.dto.channel.channelUpdateDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcChannelService;
import com.csun.cmny.provider.service.OmcFollowUpService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping(value = "/channel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcChannelController extends BaseController {

    @Resource
    private OmcChannelService omcChannelService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcFollowUpService omcFollowUpService;

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增渠道")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addChanner(@RequestBody @Valid channelSaveDto channelSaveDto, BindingResult br) {


        //校验此号码是否已经登记过
        OmcChannel channel = new OmcChannel();
        channel.setPhone(channelSaveDto.getPhone());
        channel.setGroupId(getLoginAuthDto().getGroupId());
        List<OmcChannel> list = omcChannelService.select(channel);

        if(PublicUtil.isNotEmpty(list)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("channel.phone.repeat", null));
        }

           //插入回访跟进信息
        OmcFollowUp omcFollowUp = new OmcFollowUp();
        BeanUtils.copyProperties(channelSaveDto,omcFollowUp);
        omcFollowUp.setId(null);
        omcFollowUp.setSource(0);
        omcFollowUp.setGroupId(getLoginAuthDto().getGroupId());
        int save = omcFollowUpService.save(omcFollowUp);

        if(save>0){
            OmcChannel omcChannel = new OmcChannel();
            BeanUtils.copyProperties(channelSaveDto,omcChannel);
            omcChannel.setGroupId(getLoginAuthDto().getGroupId());
            omcChannel.setReceptionist(getLoginAuthDto().getUserName());
            omcChannel.setFollowUpId(omcFollowUp.getId());

            Integer relust=omcChannelService.save(omcChannel);

            return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),relust);
        }




        return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("operation.failed", null));
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改渠道")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper update(@RequestBody @Valid channelUpdateDto channelUpdateDto, BindingResult br) {

        //校验此手机号是否在别的渠道登记过
        if(PublicUtil.isNotEmpty(channelUpdateDto.getPhone())){
            OmcChannel channel = new OmcChannel();
            channel.setPhone(channelUpdateDto.getPhone());
            channel.setGroupId(getLoginAuthDto().getGroupId());
            List<OmcChannel> select = omcChannelService.select(channel);
            if(PublicUtil.isNotEmpty(select)){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("channel.phone.repeat", null));
            }
        }


        OmcChannel channel = new OmcChannel();

        BeanUtils.copyProperties(channelUpdateDto,channel);
        Integer result=omcChannelService.update(channel);


        if(result>0){
            OmcChannel omcChannel = omcChannelService.selectByKey(channel.getId());
            OmcFollowUp omcFollowUp = omcFollowUpService.selectByKey(omcChannel.getFollowUpId());
            OmcFollowUp followUp = new OmcFollowUp();
            BeanUtils.copyProperties(omcChannel,followUp);
            followUp.setId(omcFollowUp.getId());

            omcFollowUpService.update(followUp);
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询渠道")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper select(@RequestBody ChannelQueryDto channelQueryDto) {


        channelQueryDto.setGroupId(getLoginAuthDto().getGroupId());
       List<OmcChannel>omcChannels=omcChannelService.selectChannel(channelQueryDto);

        PageInfo<OmcChannel> pageInfo = new PageInfo<>(omcChannels);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }




}
